#!/usr/bin/env bash
# set -x
set -euo pipefail

cd $(dirname ${BASH_SOURCE[0]})
SHELL_FOLDER=$(pwd)
DEPLOY_ROOT=$SHELL_FOLDER

source $DEPLOY_ROOT/utils/log.sh

INFO "work dir: $SHELL_FOLDER"
TIMESTAMP=$(date +%Y-%m-%d,%H:%M:%S)

INFO "Start to check and install ansible python environment."
bash $DEPLOY_ROOT/ansible/install_ansible_env.sh

source $DEPLOY_ROOT/init/env.sh

INFO "Execute [python3 configure.py] to configure ansible hosts and vars."
cd $SHELL_FOLDER
python3 config/configure.py

INFO "Start to generate ansible inventory.yaml"
cd $DEPLOY_ROOT/ansible/
INFO "Execute [python3 gen.py inventory]"
python3 gen.py inventory

INFO "Start to download jq/yq."
bash $DEPLOY_ROOT/tools/install_jq.sh
bash $DEPLOY_ROOT/tools/install_yq.sh

INFO "Configure Ansible."
bash $DEPLOY_ROOT/ansible/config_ansible_cfg.sh

TIMESTAMP=$(date +%Y-%m-%d,%H:%M:%S)
NOTICE "Configure Done!"
